import "./generateManifest"
import {type ConfigEnv, type UserConfigExport} from "vite"
import {resolve} from "path"


// 定义页面入口对象
const pages = {
  // main: resolve(__dirname, "./index.html"), // 主页面
  // popup: resolve(__dirname, "./popup.html"), // 弹窗页面
  // background: resolve(__dirname, "./src/background.ts"),
  content: resolve(__dirname, "./src/content.ts"),
};
const output = () => {
  return {
    assetFileNames: '[ext]/[name]-[hash].[ext]',
    entryFileNames: (chunkInfo: any) => {
      if (["background", "content"].includes(chunkInfo.name)) {
        return '[name].js';
      }
      return 'js/[name].js';
    },
    chunkFileNames: (chunkInfo: any) => {
      if (["background", "content"].includes(chunkInfo.name)) {
        return '[name].js';
      }
      return `js/[name]-[hash].js`;
    },
    manualChunks: (path: string) => {
      if (path.includes("src/background.ts")) {
        return "background";
      }
      if (path.includes("src/content.ts")) {
        return "content";
      }
    },
  };
};

export default ({mode}: ConfigEnv): UserConfigExport => {
  return {
    base: './',
    publicDir: resolve(__dirname, './public'),
    build: {
      outDir: resolve(__dirname, "./dist"),
      rollupOptions: {
        input: pages,
        output: output()
      },
      minify: true,
    }
  }
}
